package logic.nodes.damagable.shield;

import settings.GraphicSettings;
import logic.nodes.damagable.DamagableNode;
import main.InitGame;
import main.listeners.listenerClasses.SettingsListener;

import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.GLSLShaderObjectsState;

import fileHandling.ShaderLoader;
import fileHandling.language.options.OptionValues;

public class ShieldSphere extends ShieldEffectObject {
	
	private static final long serialVersionUID = 1L;
	
	public static final int R_SAMPLES_HIGH = 64;
	public static final int R_SAMPLES_MED = 32;
	public static final int R_SAMPLES_LOW = 16;
	
	public static final int Z_SAMPLES_HIGH = 32;
	public static final int Z_SAMPLES_MED = 16;
	public static final int Z_SAMPLES_LOW = 8;
	
	private static final String SPHERE_NAME = "ShieldSphere";
	
	public static int r_samples = R_SAMPLES_LOW;
	public static int z_samples = Z_SAMPLES_LOW;
	
	private static SettingsListener settingsListener;
	
	private GLSLShaderObjectsState so;
	
	public ShieldSphere(DamagableNode node, Shields shields, Vector3f location, float maxDist) {
		super(node, shields, location, maxDist);
		
		so = ShaderLoader.getShieldShader();
		so.setUniform("collision", location);
		so.setUniform("maxDist", maxDist);
		so.setUniform("degradation", 0f);
		object.setRenderState(so);
		object.updateRenderState();
		
		if(settingsListener == null) {
			settingsListener = new ShieldSettingsListener();
			InitGame.get().addSettingsListener(settingsListener);
			updateQuality();
		}
	}
	
	public static void updateQuality() {
		String geomQual = GraphicSettings.get().getGeometryQualityString();
		if(geomQual.equals(OptionValues.High.toString())) {
			r_samples = R_SAMPLES_HIGH;
			z_samples = Z_SAMPLES_HIGH;
		} else if(geomQual.equals(OptionValues.Medium.toString())) {
			r_samples = R_SAMPLES_MED;
			z_samples = Z_SAMPLES_MED;
		} else {
			r_samples = R_SAMPLES_LOW;
			z_samples = Z_SAMPLES_LOW;
		}
	}
	
	@Override
	public void setAlpha(float alpha) {
		so.setUniform("degradation", alpha);
	}
	
	@Override
	protected TriMesh getObject() {
		float radius = node.getShieldBound().getRadius();
		return new Sphere(SPHERE_NAME, z_samples, r_samples, radius);
	}
}